home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Ebooks / Thinking in Java / c13 / GoodTechnique.java < prev    next >
Encoding:
Java Source  |  2000-05-25  |  8.4 KB  |  277 lines

  1. //: GoodTechnique.java
  2. //////////////////////////////////////////////////
  3. // Copyright (c) Bruce Eckel, 1998
  4. // Source code file from the book "Thinking in Java"
  5. // All rights reserved EXCEPT as allowed by the
  6. // following statements: You can freely use this file
  7. // for your own work (personal or commercial),
  8. // including modifications and distribution in
  9. // executable form only. Permission is granted to use
  10. // this file in classroom situations, including its
  11. // use in presentation materials, as long as the book
  12. // "Thinking in Java" is cited as the source. 
  13. // Except in classroom situations, you cannot copy
  14. // and distribute this code; instead, the sole
  15. // distribution point is http://www.BruceEckel.com 
  16. // (and official mirror sites) where it is
  17. // freely available. You cannot remove this
  18. // copyright and notice. You cannot distribute
  19. // modified versions of the source code in this
  20. // package. You cannot use this file in printed
  21. // media without the express permission of the
  22. // author. Bruce Eckel makes no representation about
  23. // the suitability of this software for any purpose.
  24. // It is provided "as is" without express or implied
  25. // warranty of any kind, including any implied
  26. // warranty of merchantability, fitness for a
  27. // particular purpose or non-infringement. The entire
  28. // risk as to the quality and performance of the
  29. // software is with you. Bruce Eckel and the
  30. // publisher shall not be liable for any damages
  31. // suffered by you or any third party as a result of
  32. // using or distributing software. In no event will
  33. // Bruce Eckel or the publisher be liable for any
  34. // lost revenue, profit, or data, or for direct,
  35. // indirect, special, consequential, incidental, or
  36. // punitive damages, however caused and regardless of
  37. // the theory of liability, arising out of the use of
  38. // or inability to use software, even if Bruce Eckel
  39. // and the publisher have been advised of the
  40. // possibility of such damages. Should the software
  41. // prove defective, you assume the cost of all
  42. // necessary servicing, repair, or correction. If you
  43. // think you've found an error, please email all
  44. // modified files with clearly commented changes to:
  45. // Bruce@EckelObjects.com. (Please use the same
  46. // address for non-code errors found in the book.)
  47. /////////////////////////////////////////////////
  48.  
  49. // Your first choice when overriding components
  50. // should be to install listeners. The code is
  51. // much safer, more modular and maintainable.
  52. import java.awt.*;
  53. import java.awt.event.*;
  54.  
  55. class Display {
  56.   public static final int
  57.     EVENT = 0, COMPONENT = 1,
  58.     MOUSE = 2, MOUSE_MOVE = 3,
  59.     FOCUS = 4, KEY = 5, ACTION = 6,
  60.     LAST = 7;
  61.   public String[] evnt;
  62.   Display() {
  63.     evnt = new String[LAST];
  64.     for(int i = 0; i < LAST; i++)
  65.       evnt[i] = new String();
  66.   }
  67.   public void show(Graphics g) {
  68.     for(int i = 0; i < LAST; i++)
  69.       g.drawString(evnt[i], 0, 10 * i + 10);
  70.   }
  71. }
  72.  
  73. class EnabledPanel extends Panel {
  74.   Color c;
  75.   int id;
  76.   Display display = new Display();
  77.   public EnabledPanel(int i, Color mc) {
  78.     id = i;
  79.     c = mc;
  80.     setLayout(new BorderLayout());
  81.     add(new MyButton(), BorderLayout.SOUTH);
  82.     addComponentListener(new CL());
  83.     addFocusListener(new FL());
  84.     addKeyListener(new KL());
  85.     addMouseListener(new ML());
  86.     addMouseMotionListener(new MML());
  87.   }
  88.   // To eliminate flicker:
  89.   public void update(Graphics g) {
  90.     paint(g);
  91.   }
  92.   public void paint(Graphics  g) {
  93.     g.setColor(c);
  94.     Dimension s = getSize();
  95.     g.fillRect(0, 0, s.width, s.height);
  96.     g.setColor(Color.black);
  97.     display.show(g);
  98.   }
  99.   // Don't need to enable anything for this:
  100.   public void processEvent(AWTEvent e) {
  101.     display.evnt[Display.EVENT]= e.toString();
  102.     repaint();
  103.     super.processEvent(e);
  104.   }
  105.   class CL implements ComponentListener {
  106.     public void componentMoved(ComponentEvent e){
  107.       display.evnt[Display.COMPONENT] = 
  108.         "Component moved";
  109.       repaint();
  110.     }
  111.     public void 
  112.     componentResized(ComponentEvent e) {
  113.       display.evnt[Display.COMPONENT] = 
  114.         "Component resized";
  115.       repaint();
  116.     }
  117.     public void 
  118.     componentHidden(ComponentEvent e) {
  119.       display.evnt[Display.COMPONENT] = 
  120.         "Component hidden";
  121.       repaint();
  122.     }
  123.     public void componentShown(ComponentEvent e){
  124.       display.evnt[Display.COMPONENT] = 
  125.         "Component shown";
  126.       repaint();
  127.     }
  128.   }
  129.   class FL implements FocusListener {
  130.     public void focusGained(FocusEvent e) {
  131.       display.evnt[Display.FOCUS] = 
  132.         "FOCUS gained";
  133.       repaint();
  134.     }
  135.     public void focusLost(FocusEvent e) {
  136.       display.evnt[Display.FOCUS] = 
  137.         "FOCUS lost";
  138.       repaint();
  139.     }
  140.   }
  141.   class KL implements KeyListener {
  142.     public void keyPressed(KeyEvent e) {
  143.       display.evnt[Display.KEY] = 
  144.         "KEY pressed: "; 
  145.       showCode(e);
  146.     }
  147.     public void keyReleased(KeyEvent e) {
  148.       display.evnt[Display.KEY] = 
  149.         "KEY released: "; 
  150.       showCode(e);
  151.     }
  152.     public void keyTyped(KeyEvent e) {
  153.       display.evnt[Display.KEY] = 
  154.         "KEY typed: ";
  155.       showCode(e);
  156.     }
  157.     void showCode(KeyEvent e) {
  158.       int code = e.getKeyCode();
  159.       display.evnt[Display.KEY] += 
  160.         KeyEvent.getKeyText(code);
  161.       repaint();
  162.     }
  163.   }
  164.   class ML implements MouseListener {
  165.     public void mouseClicked(MouseEvent e) {
  166.       requestFocus(); // Get FOCUS on click
  167.       display.evnt[Display.MOUSE] = 
  168.         "MOUSE clicked";
  169.       showMouse(e);
  170.     }
  171.     public void mousePressed(MouseEvent e) {
  172.       display.evnt[Display.MOUSE] = 
  173.         "MOUSE pressed";
  174.       showMouse(e);
  175.     }
  176.     public void mouseReleased(MouseEvent e) {
  177.       display.evnt[Display.MOUSE] = 
  178.         "MOUSE released";
  179.       showMouse(e);
  180.     }
  181.     public void mouseEntered(MouseEvent e) { 
  182.       display.evnt[Display.MOUSE] = 
  183.         "MOUSE entered";
  184.       showMouse(e);
  185.     }
  186.     public void mouseExited(MouseEvent e) { 
  187.       display.evnt[Display.MOUSE] = 
  188.         "MOUSE exited";
  189.       showMouse(e);
  190.     }
  191.     void showMouse(MouseEvent e) {
  192.       display.evnt[Display.MOUSE] += 
  193.         ", x = " + e.getX() + 
  194.         ", y = " + e.getY();
  195.       repaint();
  196.     }
  197.   }
  198.   class MML implements MouseMotionListener {
  199.     public void mouseDragged(MouseEvent e) {
  200.       display.evnt[Display.MOUSE_MOVE] = 
  201.         "MOUSE dragged";
  202.       showMouse(e);
  203.     }
  204.     public void mouseMoved(MouseEvent e) {
  205.       display.evnt[Display.MOUSE_MOVE] = 
  206.         "MOUSE moved";
  207.       showMouse(e);
  208.     }
  209.     void showMouse(MouseEvent e) {
  210.       display.evnt[Display.MOUSE_MOVE] += 
  211.         ", x = " + e.getX() + 
  212.         ", y = " + e.getY();
  213.       repaint();
  214.     }
  215.   }
  216. }
  217.  
  218. class MyButton extends Button {
  219.   int clickCounter;
  220.   String label = "";
  221.   public MyButton() {
  222.     addActionListener(new AL());
  223.   }
  224.   public void paint(Graphics g) {
  225.     g.setColor(Color.green);
  226.     Dimension s = getSize();
  227.     g.fillRect(0, 0, s.width, s.height);
  228.     g.setColor(Color.black);
  229.     g.drawRect(0, 0, s.width - 1, s.height - 1);
  230.     drawLabel(g);
  231.   }
  232.   private void drawLabel(Graphics g) {
  233.     FontMetrics fm = g.getFontMetrics();
  234.     int width = fm.stringWidth(label);
  235.     int height = fm.getHeight();
  236.     int ascent = fm.getAscent();
  237.     int leading = fm.getLeading();
  238.     int horizMargin = 
  239.       (getSize().width - width)/2;
  240.     int verMargin = 
  241.       (getSize().height - height)/2;
  242.     g.setColor(Color.red);
  243.     g.drawString(label, horizMargin, 
  244.       verMargin + ascent + leading);
  245.   }
  246.   class AL implements ActionListener {
  247.     public void actionPerformed(ActionEvent e) {
  248.       clickCounter++;
  249.       label = "click #" + clickCounter +
  250.         " " + e.toString();
  251.       repaint();
  252.     }
  253.   }
  254. }
  255.   
  256. public class GoodTechnique extends Frame {
  257.   GoodTechnique() {
  258.     setLayout(new GridLayout(2,2));
  259.     add(new EnabledPanel(1, Color.cyan));
  260.     add(new EnabledPanel(2, Color.lightGray));
  261.     add(new EnabledPanel(3, Color.yellow));
  262.   }
  263.   public static void main(String[] args) {
  264.     Frame f = new GoodTechnique();
  265.     f.setTitle("Good Technique");
  266.     f.addWindowListener(
  267.       new WindowAdapter() {
  268.         public void windowClosing(WindowEvent e){
  269.           System.out.println(e);
  270.           System.out.println("Window Closing");
  271.           System.exit(0);
  272.         }
  273.       });
  274.     f.setSize(700,700);
  275.     f.setVisible(true);
  276.   }
  277. } ///:~